<?php

namespace app\common\behavior;


use app\common\library\Img;
use think\Exception;
use think\exception\ValidateException;

class JuheSms
{
    protected $sendUrl;
    protected $key;
    protected $tpl_id_single;
    public function __construct() {
        $this->sendUrl = config('site.juhe_href');
        $this->key = config('site.juhe_key');
        $this->tpl_id_single = config('site.tpl_id_single');
        $this->juhe_piliang_id = config('site.juhe_piliang_id');
    }
    /* 该方法 单独使用 不是验证码类型*/
    public function smsPiliangSend(&$data)
    {
        foreach ($data as $k => $v) {
            $smsConf = array(
                'key'   => $this->key, //您申请的APPKEY
                'mobile'    => $v['mobile'], //接受短信的用户手机号码
                'tpl_id'    => $this->juhe_piliang_id, //您申请的短信模板ID，根据实际情况修改
            );

            $content = $this->juhecurl($this->sendUrl,$smsConf,1); //请求发送短信  发送短信  不管心成功
        }

    }

    public function smsSend($sms) {
        $smsConf = array(
            'key'   => $this->key, //您申请的APPKEY
            'mobile'    => $sms['mobile'], //接受短信的用户手机号码
            'tpl_id'    => $this->tpl_id_single, //您申请的短信模板ID，根据实际情况修改
            'tpl_value' =>'#code#='.$sms['code'] //您设置的模板变量，根据实际情况修改
        );

        $content = $this->juhecurl($this->sendUrl,$smsConf,1); //请求发送短信
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //状态为0，说明短信发送成功
                return true;
            }else{
                //状态非0，说明失败
                return false;
            }
        }else{
            //返回内容异常，以下可根据业务逻辑自行修改
            return false;
        }
    }
    /**
     * 检测验证是否正确
     * @param   Sms     $params
     * @return  boolean
     */
    public function smsCheck(&$params)
    {
        return TRUE;
    }


    //聚合实名认证
    public function juheRealName(&$data) {
        header('Content-type:text/html;charset=utf-8');
        //配置您申请的appkey
        $appkey = config('site.juhe_idcard_key');
        //************1.真实姓名和身份证号码判断是否一致************
        $url = config('site.juhe_idcard');
        $params = array(
            "idcard" => $data['identity_card'],//身份证号码
            "realname" => $data['real_name'],//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
        );

        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);

        if($result){
            if($result['error_code']=='0'){
                if($result['result']['res'] == '1'){
                    return true;
                }else{
                    throw new ValidateException("身份证号码和真实姓名一致");
                }
                #print_r($result);
            }else{
                throw new ValidateException($result['error_code'].":".$result['reason']);
            }
        }else{
            throw new ValidateException('请求失败');
        }
    }

    //聚合银行卡认证
    public function juheBankCard(&$data) {
        header('Content-type:text/html;charset=utf-8');
        //配置您申请的appkey
        $appkey = config('site.juhe_bank_api_key');
        //************1.真实姓名和身份证号码判断是否一致************
        $url = config('site.juhe_bank_card');
        $params = array(
            "idcard" => $data['identity_card'],//身份证号码
            "realname" => $data['real_name'],//真实姓名
            "key" => $appkey,//应用APPKEY(应用详细页查询)
            "bankcard" => $data['bank_card'],
            "mobile" => $data['mobile'],
        );

        $paramstring = http_build_query($params);
        $content = $this->juhecurl($url,$paramstring);
        $result = json_decode($content,true);

        if($result){
            if($result['error_code']=='0'){
                if($result['result']['res'] == '1'){
                    return true;
                }else{
                    throw new ValidateException("请检查姓名，身份证号码，卡号，注册手机号是否和银行预留一致");
                }
                #print_r($result);
            }else{
                throw new ValidateException($result['error_code'].":".$result['reason']);
            }
        }else{
            throw new ValidateException('请求失败');
        }
    }

    //聚合图片提取 身份证ocr认证
    public function juheImg(&$data) {
        header('Content-type:text/html;charset=utf-8');
        $url = 'http://apis.juhe.cn/idimage/verify';
        //配置您申请的appkey
        $appkey = '6c974067469dbee6717254025339a90a';
        $base64 = (new Img())->getBase64($data['img']);
        $params = array(
            "side" => $data['type'],//身份证号码
            "key" => $appkey,//应用APPKEY(应用详细页查询)
            "image" => $base64,//真实姓名
        );

        $content = $this->juhecurl($url,$params, 1);
        $result = json_decode($content,true);

        if($result){
            if($result['error_code']=='0'){
                 if (!$result['result']) {
                     throw new ValidateException('证件不正确，请重新上传');
                 }

                 return $result['result'];
            }else{
                throw new ValidateException($result['error_code'].":".$result['reason']);
            }
        }else{
            throw new ValidateException('认证信息出错，请查看证件图片');
        }

    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    private function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }

}
